﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include <QPushButton>
#include <QHBoxLayout>
#include <QSignalMapper>

// custom headers
#include "BottomLineTab.h"
#include "BottomLineButton.h"
#include "GbmpNew.h"
#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    BottomLineTab::BottomLineTab(QWidget * parent) : CustomTab(parent)
    {
        Init();
    }

    BottomLineTab::~BottomLineTab()
    {

    }

    QWidget * BottomLineTab::CreateHeaderWidget(QString header)
    {
        BottomLineButton *pHeaderButton = NEW_AS_QT(BottomLineButton,header);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pHeaderButton, L"pHeaderButton为空", L"GMEP", L"2024-01-30");
        pHeaderButton->SetShowBottomLine(false);
        pHeaderButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
        QFont font = pHeaderButton->font();
        font.setFamily("Microsoft YaHei");
        font.setPixelSize(14);
        pHeaderButton->setFont(font);
        QPalette palette = pHeaderButton->palette();
        palette.setColor(QPalette::ColorRole::Foreground, QColor(0, 0, 0, 255));
        pHeaderButton->setPalette(palette);

        connect(pHeaderButton, &BottomLineButton::clicked, m_pHeadersSignalMapper, static_cast<void (QSignalMapper:: *)()>(&QSignalMapper::map));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(m_pHeadersSignalMapper, L"m_pHeadersSignalMapper为空", L"GMEP", L"2024-01-30");
        m_pHeadersSignalMapper->setMapping(pHeaderButton, pHeaderButton);

        return pHeaderButton;
    }

    void BottomLineTab::UpdateHeaderWidget(QWidget * headerWidget, int headerStyleFlag)
    {
        if (headerWidget)
        {
            BottomLineButton *bottomLineButton = dynamic_cast<BottomLineButton *>(headerWidget);
            if (bottomLineButton)
            {
                if (headerStyleFlag & HeaderStyleFlag::Current)
                {
                    bottomLineButton->SetShowBottomLine(true);
                    QPalette palette = bottomLineButton->palette();
                    palette.setColor(QPalette::ColorRole::Foreground, QColor(0, 0, 0, 255));
                    bottomLineButton->setPalette(palette);
                }
                else
                {
                    bottomLineButton->SetShowBottomLine(false);
                    QPalette palette = bottomLineButton->palette();
                    palette.setColor(QPalette::ColorRole::Foreground, QColor(0, 0, 0, 255));
                    bottomLineButton->setPalette(palette);
                }
            }
        }
    }

    void BottomLineTab::Init()
    {
        m_pHeadersSignalMapper = NEW_AS_QT(QSignalMapper,this);
        connect(m_pHeadersSignalMapper, static_cast<void (QSignalMapper:: *)(QWidget *)>(&QSignalMapper::mapped), this, &BottomLineTab::SwtichToTab);

        CustomTab::SetHeaderAlignment(Qt::AlignLeft);
    }
}